package core

import (
	"context"
	clientv3 "go.etcd.io/etcd/client/v3"
)

type EtcdRegistrar struct {
	conf      *ClusterConf
	cli       *clientv3.Client
	node      string
	bindAddr  string
	leaseId   clientv3.LeaseID
	leaseChan <-chan *clientv3.LeaseKeepAliveResponse
}

func NewEtcdRegistrar() (registrar *EtcdRegistrar, err error) {
	return &EtcdRegistrar{}, nil
}
func (registrar *EtcdRegistrar) Register(ctx context.Context, node *Node) error {
	return nil
}
func (registrar *EtcdRegistrar) Deregister(ctx context.Context, node *Node) error {
	return nil
}

//func (registrar *EtcdRegistrar) Close() {
//	if registrar == nil {
//		return
//	}
//
//	if registrar.cli == nil {
//		return
//	}
//
//	_ = registrar.cli.Close()
//}

//
//func (adapter *EtcdRegistrar) Connect(conf *ClusterConf) (err error) {
//	if adapter == nil {
//		return errors.New("invalidate adapter")
//	}
//	adapter.cli, err = clientv3.New(clientv3.Config{
//		Endpoints:   conf.CenterEndPoints,
//		DialTimeout: conf.DialTimeout,
//	})
//
//	if err != nil {
//		log.Error("can not connect to etcd endpoint:", conf.CenterEndPoints, err.Error())
//		return err
//	}
//	//var session *concurrency.Session
//	//session, err = concurrency.NewSession(conn.cli, concurrency.WithTTL(5))
//	return nil
//}
//
//func (adapter *EtcdRegistrar) Register(nodeName string, bindAddr string, watch RegisterWatch) (nodeId uint32, err error) {
//	if adapter == nil {
//		return 0, errors.New("invalidate adapter")
//	}
//
//	watchPath := fmt.Sprintf("%s/Clusters/%s", NameSpace, adapter.conf.Name)
//
//	lease := clientv3.NewLease(adapter.cli) // 创建一个租约
//
//	leaseResp, err := lease.Grant(context.Background(), adapter.conf.Keepalive) // 设置租约超时 10秒
//	if err != nil {
//		fmt.Println(err)
//		return
//	}
//	leaseRespChan, _ := lease.KeepAlive(context.Background(), leaseResp.ID) // 通过keeplive定期发送心跳
//	kv := clientv3.NewKV(adapter.cli)
//	key := fmt.Sprintf(watchPath, leaseResp.ID)
//	value := `{"host": "192.168.0.1", "port": 8000}`
//	fmt.Println("key", key)
//
//	_, err = kv.Put(context.Background(), key, value, clientv3.WithLease(leaseResp.ID))
//	if err != nil {
//		fmt.Println(err)
//		return
//	}
//
//	go func(theAdapter *EtcdRegistrar, theWatch RegisterWatch, leasChan <-chan *clientv3.LeaseKeepAliveResponse) {
//
//	}(adapter, watch, leaseRespChan)
//	return
//}
//
//func (adapter *EtcdAdapter) DeRegister() {
//
//}
//func (adapter *EtcdAdapter) GetNodes() (nodes []*Node, err error) {
//	return
//}
